//
//  MBSpaceMyParentChildShowPlayViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/20.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceMyParentChildShowPlayViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "MBLoginController.h"
#import "AppDelegate.h"
#import "Music.h"
#import <MediaPlayer/MediaPlayer.h>

@interface MBSpaceMyParentChildShowPlayViewController () {
    UIScrollView                        *_scrollView;
    UIView                              *_navBarView;
    UIButton                            *_playeringBt;
    UISlider                            *_progressView;
    UILabel                             *_currentTimeLb;  //当前时间
    UILabel                             *_totalTimeLb;    //总时间
    UIImageView                         *_userImgView;     //头像
    UILabel                             *_nickNameLb;      //昵称
    UILabel                             *_nickDetailLb;     //个人介绍
    UILabel                             *_contentLb;
    UIView                              *bottomView;
    UIView                              *topView;
    UILabel                             *titleLb;
    UIButton                            *_downBt;
    UIImageView                         *_bgImgView;
    NSMutableArray                      *_musicArray;       //音频数组
    NSInteger                           selectCurrentIndex; //选中当前第几个播放音频
    AVPlayer                            *_audioPlayer;
    BOOL                                isFromBackGround;   //是否从后台跳入
    NSTimer                             *_showTimer;
    BOOL                                isCircle;      //是否自动播放下一首歌
    BOOL isPlay;                                        //是否处于播放状态

    Music                               *currentMusic;
    PLayerModelType                     playModelType; //音频播放类型
    NSMutableArray                      *fileIDArr;   //下载音频数组
    NSMutableDictionary                 *_selectPraiseDic;  //点赞、收藏等
    
     CLShareManager *shareView;
    AVAudioSession                      *_audioSession;
}

@end

@implementation MBSpaceMyParentChildShowPlayViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
    //离开判断播放器是否正在播放
    if (_showTimer && [_showTimer isValid]) {
        [[UserManager standerDefult] setMainPlayer:_audioPlayer];
        [[UserManager standerDefult]setIsBackPlay:_playeringBt.selected];
        
        [_showTimer invalidate];
        _showTimer = nil;
    } else {
        [[UserManager standerDefult] setMainPlayer:_audioPlayer];
        [[UserManager standerDefult]setIsBackPlay:NO];
    }
    
    
    [[UserManager standerDefult]setSelectMusicIndex:selectCurrentIndex];
    [[UserManager standerDefult]setMusicArr:_musicArray];
    [[UserManager standerDefult]setIsPlaying:isPlay];
    [[UserManager standerDefult]setAudioPlayerType:AudioPLayerType_ZhuBo];
    [[UserManager standerDefult]setZhuboDic:(NSMutableDictionary *)_dataDic];
    [[UserManager standerDefult]setIsPlaying:_playeringBt.selected];
    [[UserManager standerDefult]setIsBackPlay:(_playeringBt.selected == YES?YES:NO)];
    [[UserManager standerDefult]setMusicPlayType:MusicPLayType_QinZiXiu];
    [[UserManager standerDefult]setIsLastID:[NSString stringWithFormat:@"%@",currentMusic.ID]];
    [[UserManager standerDefult]setMusicPlayType:MusicPLayType_Collection];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemFailedToPlayToEndTimeNotification object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    isPlay = YES;            //是否处于播放状态
    isCircle = YES;          //判断是否允许循环播放
    selectCurrentIndex = 0;    //歌曲列表中第几条
    _musicArray = [[NSMutableArray alloc]init];
    _selectPraiseDic = [[NSMutableDictionary alloc]init];
    shareView = [[CLShareManager alloc] init];
    
    //UI
    [self setBackItem];
    [self onCreateUI];
    
    [self onLoadData];
    
}

#pragma mark - Actions
#pragma mark - UI
- (void)setBackItem
{
    //全局
    if (IOS7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-100*PERCEN)];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    //导航
    _navBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    _navBarView.backgroundColor = [UIColor clearColor];
    [_scrollView addSubview:_navBarView];
    
    UIImage *img = [UIImage imageNamed:@"fanhui"];
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(10, (44-img.size.height)/2., 80, img.size.height);
    [back setImage:img forState:UIControlStateNormal];
    [back setImage:img forState:UIControlStateHighlighted];
    [back setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 80-img.size.width)];
    [back addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside];
    [_navBarView addSubview:back];
    
    titleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 7, SCREEN_WIDTH, 30)];
    titleLb.textAlignment = NSTextAlignmentCenter;
    titleLb.textColor = [UIColor whiteColor];
    [_navBarView addSubview:titleLb];
    
    if (self.playTyle == MBSpaceMyParentChildShowPlayType_My) {        
        UIButton *sendQZShowBt = [UIButton buttonWithType:UIButtonTypeCustom];
        sendQZShowBt.frame = CGRectMake(SCREEN_WIDTH-85, titleLb.top, 80, 30);
        [sendQZShowBt setImage:[UIImage imageNamed:@"fenxiang_bs"] forState:UIControlStateNormal];
        [sendQZShowBt setImage:[UIImage imageNamed:@"fenxiang_bs"] forState:UIControlStateHighlighted];
        sendQZShowBt.backgroundColor = [UIColor clearColor];
        [sendQZShowBt setImageEdgeInsets:UIEdgeInsetsMake(0, 40, 0, 0)];
        [sendQZShowBt addTarget:self action:@selector(onDeleteMusicAction) forControlEvents:UIControlEventTouchUpInside];
        [_navBarView addSubview:sendQZShowBt];
    }
}

- (void)onCreateUI
{
    //1.上部分
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 740*PERCEN+30)];
    topView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:topView];
    [_scrollView insertSubview:_navBarView aboveSubview:topView];
    
    //背景图
    UIImageView *bgImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, topView.width, topView.height-30)];
    bgImgView.image = [UIImage imageNamed:@"qzx_musicplayer_bg"];
    [topView addSubview:bgImgView];
    _bgImgView = bgImgView;
    
    //不透明底图
    UIView *alpheBottomView = [[UIView alloc]initWithFrame:CGRectMake(0, topView.height-140*PERCEN-30, topView.width, 140*PERCEN)];
    alpheBottomView.backgroundColor = [UIColorFromHex(0x000000) colorWithAlphaComponent:.6];
    [topView addSubview:alpheBottomView];
    
    //播放、暂停
    UIImage *playImg = [UIImage imageNamed:@"bofang_xbtj"];
    UIImage *playingImg = [UIImage imageNamed:@"zanting_xbtj"];
    UIButton *playBt = [UIButton buttonWithType:UIButtonTypeCustom];
    playBt.backgroundColor = [UIColor clearColor];
    playBt.frame = CGRectMake(alpheBottomView.width/2.-playImg.size.width/2., (alpheBottomView.height-playImg.size.height)/2., playImg.size.width, playImg.size.height);
    [playBt setImage:playImg forState:UIControlStateNormal];
    [playBt setImage:playingImg forState:UIControlStateSelected];
    [playBt addTarget:self action:@selector(onSelectPlayedType:) forControlEvents:UIControlEventTouchUpInside];
    [alpheBottomView addSubview:playBt];
    _playeringBt = playBt;
    
    //上一个
    UIImage *lastImg = [UIImage imageNamed:@"shangyiqu"];
    UIButton *lastBt = [UIButton buttonWithType:UIButtonTypeCustom];
    lastBt.backgroundColor = [UIColor clearColor];
    lastBt.frame = CGRectMake(playBt.left-lastImg.size.width-80*PERCEN, (alpheBottomView.height-lastImg.size.height)/2., lastImg.size.width, lastImg.size.height);
    [lastBt setImage:lastImg forState:UIControlStateNormal];
    [lastBt setImage:lastImg forState:UIControlStateHighlighted];
    [lastBt addTarget:self action:@selector(onSelectLastPlaying) forControlEvents:UIControlEventTouchUpInside];
    [alpheBottomView addSubview:lastBt];
    
    //下一个
    UIImage *nextImg = [UIImage imageNamed:@"xiayiqu"];
    UIButton *nextBt = [UIButton buttonWithType:UIButtonTypeCustom];
    nextBt.backgroundColor = [UIColor clearColor];
    nextBt.frame = CGRectMake(playBt.right+80*PERCEN, (alpheBottomView.height-nextImg.size.height)/2., nextImg.size.width, nextImg.size.height);
    [nextBt setImage:nextImg forState:UIControlStateNormal];
    [nextBt setImage:nextImg forState:UIControlStateHighlighted];
    [nextBt addTarget:self action:@selector(onSelectNextPlaying) forControlEvents:UIControlEventTouchUpInside];
    [alpheBottomView addSubview:nextBt];
    
    //播放模式
    //播放模式
    NSString *modelType = [[NSUserDefaults standardUserDefaults]objectForKey:@"player_z_model_type"];
    UIImage *typeImg = [UIImage imageNamed:@"shunxu"];
    if (modelType.length == 0) {
        playModelType = PLayerModelType_Shunxu;
        typeImg = [UIImage imageNamed:@"shunxu"];
    } else {
        switch ([modelType intValue]) {
            case 1:
            {
                playModelType = PLayerModelType_Shunxu;
                typeImg = [UIImage imageNamed:@"shunxu"];
            }
                break;
            case 2:
            {
                playModelType = PLayerModelType_SuiJi;
                typeImg = [UIImage imageNamed:@"suiji"];
            }
                break;
            case 3:
            {
                playModelType = PLayerModelType_DanQu;
                typeImg = [UIImage imageNamed:@"danqu"];
            }
                break;
                
            default:
            {
                playModelType = PLayerModelType_Shunxu;
                typeImg = [UIImage imageNamed:@"shunxu"];
            }
                
                break;
        }
    }
    
    UIButton *typeBt = [UIButton buttonWithType:UIButtonTypeCustom];
    typeBt.backgroundColor = [UIColor clearColor];
    typeBt.frame = CGRectMake((lastBt.left-typeImg.size.width-40*PERCEN)/2., (alpheBottomView.height-typeImg.size.height)/2., typeImg.size.width, typeImg.size.height);
    [typeBt setImage:typeImg forState:UIControlStateNormal];
    [typeBt setImage:typeImg forState:UIControlStateHighlighted];
    [typeBt addTarget:self action:@selector(onSelectPlayType:) forControlEvents:UIControlEventTouchUpInside];
    [alpheBottomView addSubview:typeBt];
    
    //下载
    UIImage *downBtImg = [UIImage imageNamed:@"weixiazai"];
    UIImage *downSelectBtImg = [UIImage imageNamed:@"yixiazai"];
    UIImage *downedLoadingImg = [UIImage imageNamed:@"xiazaizhong"];
    UIButton *downBt = [UIButton buttonWithType:UIButtonTypeCustom];
    downBt.backgroundColor = [UIColor clearColor];
    downBt.frame = CGRectMake(nextBt.right+40*PERCEN+(SCREEN_WIDTH-nextBt.right-downBtImg.size.width-40*PERCEN)/2., (alpheBottomView.height-downBtImg.size.height)/2., downBtImg.size.width, downBtImg.size.height);
    [downBt setImage:downBtImg forState:UIControlStateNormal];
    [downBt setImage:downSelectBtImg forState:UIControlStateSelected];
    [downBt setImage:downedLoadingImg forState:UIControlStateDisabled];
    [downBt addTarget:self action:@selector(onSelectDownMusic) forControlEvents:UIControlEventTouchUpInside];
    [alpheBottomView addSubview:downBt];
    _downBt = downBt;
    
    //进度条
    UISlider *slide = [[UISlider alloc]initWithFrame:CGRectMake(0, alpheBottomView.height-2, alpheBottomView.width, 2)];
    slide.backgroundColor = [UIColor colorWithRed:193/255. green:193/255. blue:193/255. alpha:1.];
    slide.minimumValue = 0;
    slide.maximumValue = 1;
    slide.value = 0;
    slide.minimumTrackTintColor = TINK_BG_COLOR;
    slide.maximumTrackTintColor = [UIColor colorWithRed:193/255. green:193/255. blue:193/255. alpha:1.];
    [slide setThumbImage:[UIImage imageNamed:@"jiedian"] forState:UIControlStateNormal];
    [slide setThumbImage:[UIImage imageNamed:@"jiedian"] forState:UIControlStateHighlighted];
    [slide addTarget:self action:@selector(onSelectSlideValueForChangePlayMusic) forControlEvents:UIControlEventValueChanged];
    [alpheBottomView addSubview:slide];
    _progressView = slide;
    
    //当前时间
    _currentTimeLb = [[UILabel alloc]initWithFrame:CGRectMake(25*PERCEN, alpheBottomView.bottom+5, 120, 20)];
    _currentTimeLb.backgroundColor = [UIColor clearColor];
    _currentTimeLb.textAlignment = NSTextAlignmentLeft;
    _currentTimeLb.textColor = UIColorFromHex(0x999999);
    _currentTimeLb.font = [UIFont systemFontOfSize:12.f];
    _currentTimeLb.text = @"00:00";
    [topView addSubview:_currentTimeLb];
    
    //总时间
    _totalTimeLb = [[UILabel alloc]initWithFrame:CGRectMake(alpheBottomView.width-25*PERCEN-_currentTimeLb.width, _currentTimeLb.top, _currentTimeLb.width, _currentTimeLb.height)];
    _totalTimeLb.backgroundColor = [UIColor clearColor];
    _totalTimeLb.textAlignment = NSTextAlignmentRight;
    _totalTimeLb.textColor = UIColorFromHex(0x999999);
    _totalTimeLb.font = [UIFont systemFontOfSize:12.f];
    _totalTimeLb.text = @"00:00";
    [topView addSubview:_totalTimeLb];
    
    //2.身份信息
    bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, topView.bottom+10, SCREEN_WIDTH, SCREEN_HEIGHT-topView.height)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:bottomView];
    
    //头像
    _userImgView = [[UIImageView alloc]initWithFrame:CGRectMake(25*PERCEN, 30*PERCEN, 100*PERCEN, 100*PERCEN)];
    _userImgView.backgroundColor = [UIColor clearColor];
    _userImgView.layer.cornerRadius = _userImgView.height / 2.;
    NSString *faceImg = [_dataDic objectForKey:@"FaceImg"];
    if (![faceImg isKindOfClass:[NSNull class]]) {
        [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,faceImg]] placeholderImage:[UIImage imageNamed:@"xiaobian_bg"]];
    } else {
        _userImgView.image = [UIImage imageNamed:@"xiaobian_bg"];
    }
    _userImgView.layer.masksToBounds = YES;
    [bottomView addSubview:_userImgView];
    
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectMake(_userImgView.right+20*PERCEN, _userImgView.top, 200, 20)];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:13.f];
    _nickNameLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"NickName"]];
    [bottomView addSubview:_nickNameLb];
    
    _nickDetailLb = [[UILabel alloc]initWithFrame:CGRectMake(_nickNameLb.left, _nickNameLb.bottom-2, SCREEN_WIDTH-_nickNameLb.left-25*PERCEN, 40)];
    _nickDetailLb.backgroundColor = [UIColor clearColor];
    _nickDetailLb.textAlignment = NSTextAlignmentLeft;
    _nickDetailLb.textColor = UIColorFromHex(0x999999);
    _nickDetailLb.font = [UIFont systemFontOfSize:12.f];
    _nickDetailLb.numberOfLines = 2;
    _nickDetailLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"Signature"]];
    [bottomView addSubview:_nickDetailLb];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(_userImgView.left, _userImgView.bottom+10, SCREEN_WIDTH-_userImgView.left, 1)];
    lineView.backgroundColor = [UIColorFromHex(0x999999)colorWithAlphaComponent:.35];
    [bottomView addSubview:lineView];
    
    _contentLb = [[UILabel alloc]initWithFrame:CGRectMake(_userImgView.left, lineView.bottom+10, _scrollView.width-_userImgView.left*2, _scrollView.height)];
    _contentLb.backgroundColor = [UIColor clearColor];
    _contentLb.textAlignment = NSTextAlignmentLeft;
    _contentLb.textColor = UIColorFromHex(0x333333);
    _contentLb.font = [UIFont systemFontOfSize:13.f];
    _contentLb.numberOfLines = 0;
    [bottomView addSubview:_contentLb];
    
    //3点赞
    UIView *zanView = [[UIView alloc]initWithFrame:CGRectMake(0, _scrollView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-_scrollView.bottom)];
    zanView.backgroundColor = UIColorFromHex(0x000000);
    [self.view addSubview:zanView];
    
    //赞
    for (int i=0; i<2; i++) {
        
        UIImageView *topImgView = [[UIImageView alloc]initWithFrame:CGRectMake((zanView.width/2.-20)/2.+i*zanView.width/2., 5, 20, 19)];
        topImgView.backgroundColor = [UIColor clearColor];
        topImgView.tag = 10000+i;
        [zanView addSubview:topImgView];
        
        UILabel *zanLb = [[UILabel alloc]initWithFrame:CGRectMake(i*zanView.width/2., topImgView.bottom+2, zanView.width/2., zanView.height-topImgView.bottom-2)];
        zanLb.backgroundColor = [UIColor clearColor];
        zanLb.textAlignment = NSTextAlignmentCenter;
        zanLb.textColor = [UIColor whiteColor];
        zanLb.font = [UIFont systemFontOfSize:10.f];
        zanLb.tag = 11000+i;
        [zanView addSubview:zanLb];
        
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.frame = CGRectMake(i*zanView.width/2., 0, zanView.width/2., zanView.height);
        bt.backgroundColor = [UIColor clearColor];
        [bt addTarget:self action:@selector(onSelectItemForZanOrCollection:) forControlEvents:UIControlEventTouchUpInside];
        bt.tag = 700+i;
        [zanView addSubview:bt];
        
        if (i == 0) {
            topImgView.image = [UIImage imageNamed:@"zan_qzxbf"];
            zanLb.text = @"点赞(0)";
        } else {
            topImgView.image = [UIImage imageNamed:@"shoucang_qzxbf"];
            zanLb.text = @"收藏";
        }
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieToEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieToFaile:) name:AVPlayerItemFailedToPlayToEndTimeNotification object:nil];
}

- (void)onUpdateUIFOrScrollView:(NSString *)segment
{
    NSString *str =segment;
    NSMutableParagraphStyle *mutStyle = [[NSMutableParagraphStyle alloc]init];
    mutStyle.lineSpacing = 5;
    
    NSMutableAttributedString *mutAttribute = [[NSMutableAttributedString alloc]initWithString:str];
    [mutAttribute addAttribute:NSFontAttributeName value:_contentLb.font range:NSMakeRange(0, str.length)];
    [mutAttribute addAttribute:NSParagraphStyleAttributeName value:mutStyle range:NSMakeRange(0, str.length)];
    float heiht = [mutAttribute boundingRectWithSize:CGSizeMake(_contentLb.width, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:NULL].size.height+10;
    _contentLb.height = heiht;
    _contentLb.attributedText = mutAttribute;
    bottomView.height = _contentLb.bottom+10;
    _scrollView.contentSize = CGSizeMake(_scrollView.width, (_scrollView.height >= bottomView.bottom ? _scrollView.height : bottomView.bottom));
    [_scrollView insertSubview:_navBarView aboveSubview:topView];
    [_scrollView insertSubview:_navBarView aboveSubview:bottomView];
}

#pragma mark -歌曲
- (void)initDate {
    NSArray *downedMusicArr = [[FMManageObject standerDefault]onSelectLocalParentChildShowCount];
    [fileIDArr removeAllObjects];
    for (int i=0; i<downedMusicArr.count; i++) {
        NSDictionary *dic = [downedMusicArr objectAtIndex:i];
        NSString *fileID = [dic objectForKey:@"musicId"];
        [fileIDArr addObject:fileID];
    }
    
    //获取当前的歌曲信息
    if (_musicArray.count > selectCurrentIndex) {
        currentMusic = _musicArray[selectCurrentIndex];
        titleLb.text = [currentMusic name];
        [_bgImgView sd_setImageWithURL:[NSURL URLWithString:currentMusic.CoverPath] placeholderImage:[UIImage imageNamed:@"qzx_musicplayer_bg"]];
        [self onUpdateUIFOrScrollView:currentMusic.Sentiment];
    }
    
    //下载状态
    BOOL isDown = NO;
    for (NSString *fileID in fileIDArr) {
        if ([fileID isEqualToString:[NSString stringWithFormat:@"%@",currentMusic.ID]]) {
            isDown = YES;
        }
    }
//    _downBt.selected = isDown;
    if (isDown == YES) {
        _downBt.selected = YES;
    } else {
        //未已下载好 判断是否正在下载
        NSMutableArray *mMusicArr = [[NSMutableArray alloc]init];
        [mMusicArr addObjectsFromArray:[UserManager standerDefult].downingMusicCountArr];
        [mMusicArr addObjectsFromArray:[UserManager standerDefult].waitDownMusicCountArr];
        if (mMusicArr.count > 0) {
            for (DownMusicModel *model in mMusicArr) {
                if ([model.musicID intValue] == [currentMusic.ID intValue] && [model.musicType intValue] == [currentMusic.musicType intValue]) {
                    _downBt.selected = YES;
                    _downBt.enabled = NO;
                } else {
                    _downBt.selected = NO;
                    _downBt.enabled = YES;
                }
            }
        } else {
            _downBt.selected = NO;
            _downBt.enabled = YES;
        }
    }

    
    //列入最近播放
    if (currentMusic.ID) {
        FMManageObject *manager = [FMManageObject standerDefault];
        NSArray *arrs = [manager SelectTableByMusicPlayForRecentWithContentID:[NSString stringWithFormat:@"%@",currentMusic.ID]];
        if (arrs.count == 0) {
            //来吧
            [manager insertTableMusicPlayForRecentWithId:@"1" withFileName:currentMusic.name withFileID:currentMusic.ID withUrl:currentMusic.urlPath withSize:currentMusic.Capacity withType:[NSString stringWithFormat:@"%@",currentMusic.musicType] withCoverPath:[NSString stringWithFormat:@"%@",currentMusic.CoverPath] withDate:[[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"CreateTime"]] getChaTime] withSentiment:[NSString stringWithFormat:@"%@",currentMusic.Sentiment] withTitle:[NSString stringWithFormat:@"%@",currentMusic.name] withUserId:[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"UserID"]] withUserPic:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,[_dataDic objectForKey:@"FaceImg"]] withUserNick:[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"NickName"]]];
        }
    }
    
    //点赞状态
    UserModel *model = [UserManager standerDefult].userModel;
    if (model) {
        NSString *url = [NSString stringWithFormat:STORY_STATUSLISt_URL_PATH,model.UserID,currentMusic.ID,[_dataDic objectForKey:@"UserID"]];
        [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(id json) {
            if ([json isKindOfClass:[NSDictionary class]]) {
                
                [_selectPraiseDic removeAllObjects];
                [_selectPraiseDic addEntriesFromDictionary:json];
                
                NSString *CollectStatus = json[@"CollectStatus"];
                NSString *PraiseNum = json[@"PraiseNum"];
                NSString *PraiseStatus = json[@"PraiseStatus"];
                
                UIImageView *topImgView1 = (UIImageView *)[self.view viewWithTag:10000];
                UIImageView *topImgView2 = (UIImageView *)[self.view viewWithTag:10001];
                UILabel *zanLb1 = (UILabel *)[self.view viewWithTag:11000];
                UILabel *zanLb2 = (UILabel *)[self.view viewWithTag:11001];
                
                if ([PraiseStatus intValue] == 1) {
                    //点赞点亮
//                    topImgView1.image = [UIImage imageNamed:@"zan2_qzxbf"];
//                    zanLb1.textColor = TINK_COLOR;
                } else {
                    topImgView1.image = [UIImage imageNamed:@"zan_qzxbf"];
                    zanLb1.textColor = [UIColor whiteColor];
                }
                zanLb1.text = [NSString stringWithFormat:@"点赞(%@)",PraiseNum];
                if ([CollectStatus intValue] == 1) {
                    //收藏点亮
//                    topImgView2.image = [UIImage imageNamed:@"shoucang2_qzxbf"];
//                    zanLb2.textColor = TINK_COLOR;
                    zanLb2.text = @"已收藏";
                } else {
                    topImgView2.image = [UIImage imageNamed:@"shoucang_qzxbf"];
                    zanLb2.textColor = [UIColor whiteColor];
                    zanLb2.text = @"收藏";
                }
            }
        } failure:^(NSError *error) {
            
        }];
    }
    
    //当前第几首歌以及类型播放器
    [[NSUserDefaults standardUserDefaults]setObject:[NSString stringWithFormat:@"%d",(int)selectCurrentIndex] forKey:@"music_list_CurrentNum"];
    [[NSUserDefaults standardUserDefaults]setObject:[NSString stringWithFormat:@"%d",(int)3] forKey:@"music_Type_CurrentNum"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
    [self onAddMedia];
}

//锁屏显示
- (void)onAddMedia
{
    Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
    if (playingInfoCenter) {
        
        NSMutableDictionary *songInfo = [ [NSMutableDictionary alloc] init];
        MPMediaItemArtwork *albumArt = [ [MPMediaItemArtwork alloc] initWithImage:_bgImgView.image];
        
        [ songInfo setObject:currentMusic.name forKey:MPMediaItemPropertyTitle ];
        [ songInfo setObject:[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"NickName"]] forKey:MPMediaItemPropertyArtist ];
        //        [ songInfo setObject: @"Audio Album" forKey:MPMediaItemPropertyAlbumTitle ];
        [ songInfo setObject: albumArt forKey:MPMediaItemPropertyArtwork ];
        //设置歌曲时长
        CMTime duration = _audioPlayer.currentItem.duration;
        if (duration.timescale > 0) {
            double totalTime = (_audioPlayer.currentItem.duration.value / _audioPlayer.currentItem.duration.timescale);
            [songInfo setObject:[NSNumber numberWithDouble:totalTime] forKey:MPMediaItemPropertyPlaybackDuration];
        }
        //设置已经播放时长
        CMTime current = _audioPlayer.currentItem.currentTime;
        if (current.timescale > 0) {
            double currentTime = (_audioPlayer.currentItem.currentTime.value / _audioPlayer.currentItem.currentTime.timescale);
            [songInfo setObject:[NSNumber numberWithDouble:currentTime] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
        }
        
        [ [MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo ];
    }
}

- (void)setPlayer
{
    if (!_audioSession) {
        _audioSession = [AVAudioSession sharedInstance];
    }
    [_audioSession setActive:YES error:nil];
    [_audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    if ([UserManager standerDefult].mainPlayer) {
        if ([UserManager standerDefult].isPlaying) {
            _showTimer =[NSTimer scheduledTimerWithTimeInterval:0.1f
                                                         target:self
                                                       selector:@selector(showTime)
                                                       userInfo:nil
                                                        repeats:YES];
        }
        _audioPlayer = [UserManager standerDefult].mainPlayer;
        isFromBackGround = YES;
        [[UserManager standerDefult] setIsBackPlay:NO];
        
        if (![self.selectMusicID isEqualToString:[NSString stringWithFormat:@"%@",currentMusic.ID]]) {
            NSURL *urlPath = [NSURL URLWithString:[NSString stringWithFormat:@"%@",currentMusic.urlPath]];
            [_audioPlayer replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithURL:urlPath]];
        }
        
        //数据
        _playeringBt.selected = [UserManager standerDefult].isPlaying;
        if (_playeringBt.selected) {
            [_audioPlayer play];
        } else {
            [_audioPlayer pause];
        }
        isPlay = !_playeringBt.selected;
    } else {
        NSURL *urlPath;
        if (currentMusic.isCacheData) {
            NSString *filePath = [MusicCachesDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",currentMusic.name,currentMusic.type]];
            urlPath = [NSURL fileURLWithPath:filePath];
        } else {
            urlPath = [NSURL URLWithString:currentMusic.urlPath];
        }
        
        AVPlayerItem *item =[AVPlayerItem playerItemWithURL:urlPath];
        _audioPlayer = [[AVPlayer alloc]initWithPlayerItem:item];
        isFromBackGround = NO;
    }

    [self showTime];
}

#pragma mark -加载数据
- (void)onLoadData
{
    NSString *url = [NSString stringWithFormat:STORY_GET_ANCHORAUDIO_URL_PATH,[_dataDic objectForKey:@"UserID"]];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[HTTPManager standerDefault]AFGetRequest:url view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([json isKindOfClass:[NSDictionary class]]) {
            if ([json[@"ret_code"] integerValue]==0) {
                NSArray *arr = json[@"data"];
                if ([arr isKindOfClass:[NSArray class]]) {
                    [_musicArray removeAllObjects];
                    [self onUpdateMusicArr:arr];
                }
            } else {
                [ErrorMessage showErrorMessage:@"暂无歌曲" inView:self.view];
            }
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    }];
}

//将音频转化成music
- (void)onUpdateMusicArr:(NSArray *)arr
{
    for (NSDictionary *dic in arr) {
        Music *music = [[Music alloc]init];
        music.name = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ShowTitle"]];
        NSString *filePath = [dic objectForKey:@"FilePath"];
        NSString *type = @"";
        if (![filePath isKindOfClass:[NSNull class]]) {
            type = [[filePath componentsSeparatedByString:@"."]lastObject];
            
            music.type = type;
            music.urlPath = [NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,filePath];
            NSArray *qzxArr = [[FMManageObject standerDefault]SelectTableByLocalParentChildShowWithUserId:[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"UserID"]] withMusicId:[NSString stringWithFormat:@"%@",[dic objectForKey:@"ID"]]];
            music.isCacheData = (qzxArr.count > 0 ? YES : NO);
            
        }
        music.ID = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ID"]];
        music.musicType = @"6";//主播
        music.AudioPath = [NSString stringWithFormat:@"%@",filePath];
        music.Capacity = [NSString stringWithFormat:@"%@",[dic objectForKey:@"Capacity"]];
        music.Sentiment = [NSString stringWithFormat:@"%@",[dic objectForKey:@"Sentiment"]];
        music.AudioStatus = @"1";
        music.LyricPath = @"";
        NSString *CoverPath = [dic objectForKey:@"CoverPath"];
        if (![CoverPath isKindOfClass:[NSNull class]]) {
            music.CoverPath = [NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,CoverPath];
        }
        [_musicArray addObject:music];
    }
    
    //已下载歌曲记录
    NSArray *downedMusicArr = [[FMManageObject standerDefault]onSelectLocalParentChildShowCount];
    fileIDArr = [[NSMutableArray alloc]initWithCapacity:downedMusicArr.count];
    for (int i=0; i<downedMusicArr.count; i++) {
        NSDictionary *dic = [downedMusicArr objectAtIndex:i];
        NSString *fileID = [dic objectForKey:@"musicId"];
        [fileIDArr addObject:fileID];
    }
    
    //时时更新
    selectCurrentIndex = [UserManager standerDefult].selectMusicIndex;
    [self initDate];
    
    //人气歌曲列表保存
    [NSKeyedArchiver archiveRootObject:_musicArray toFile:[MusicCachesDirectory stringByAppendingPathComponent:@"collectionMusic.data"]];
    [NSKeyedArchiver archiveRootObject:_dataDic toFile:[MusicCachesDirectory stringByAppendingPathComponent:@"last_qinzixiu.data"]];
    
    [self setPlayer];
}

#pragma mark - 滑动进度条
- (void)onSelectSlideValueForChangePlayMusic
{
    CGFloat duration = _audioPlayer.currentItem.duration.value / _audioPlayer.currentItem.duration.timescale;
    NSInteger dragedSeconds = floorf(duration*_progressView.value);
    
    //转换成CMTime才能给player来控制播放进度
    CMTime dragedCMTime = CMTimeMake(dragedSeconds, 1);
    
    [_audioPlayer pause];
    [_showTimer invalidate];
    _showTimer = nil;
    [_audioPlayer seekToTime:dragedCMTime completionHandler:^(BOOL finish){
        if (finish) {
            if (!_showTimer) {
                //设置监控 每秒刷新一次时间
                _showTimer =[NSTimer scheduledTimerWithTimeInterval:0.1f
                                                             target:self
                                                           selector:@selector(showTime)
                                                           userInfo:nil
                                                            repeats:YES];
            }
            [_audioPlayer play];
        }
        
    }];

}

#pragma mark -方法
- (void)onBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

//分享
- (void)onDeleteMusicAction
{
    
    NSNumber *num = @3;
    NSString *audioName = [NSString stringWithFormat:@"%@",currentMusic.name];
    NSString *idStr = [NSString stringWithFormat:@"%@",currentMusic.ID];
    NSString *url = url = [NSString stringWithFormat:SHARE_SHOW_URL_PATH,idStr];

    audioName = [[audioName componentsSeparatedByString:@"."]firstObject];
    [shareView setShareVC:self content:[NSString stringWithFormat:@"分享:%@",audioName] image:[UIImage imageNamed:@"logo_dl_29"] url:url];
    [shareView addAudioId:[NSString stringWithFormat:@"%@",idStr] withType:num];
    [shareView show];
}

#pragma mark - 播放、暂停
- (void)onSelectPlayedType:(UIButton *)bt
{
    bt.selected = !bt.selected;
    if (bt.selected) {
        if (!_showTimer) {
            //设置监控 每秒刷新一次时间
            _showTimer =[NSTimer scheduledTimerWithTimeInterval:0.1f
                                                         target:self
                                                       selector:@selector(showTime)
                                                       userInfo:nil
                                                        repeats:YES];
        }
        [_audioPlayer play];
        isPlay = YES;
    } else {
         [_audioPlayer pause];
        isPlay = NO;
        if (_showTimer) {
            [_showTimer invalidate];
            _showTimer = nil;
        }
    }
}

- (void)showTime
{
    int currentTime = 0;
    int durationTime = 0;
    if (_audioPlayer.currentItem.currentTime.timescale != 0) {
        currentTime = (int)(_audioPlayer.currentItem.currentTime.value / _audioPlayer.currentItem.currentTime.timescale);
    }
    if (_audioPlayer.currentItem.duration.timescale != 0) {
        durationTime = (int) (_audioPlayer.currentItem.duration.value / _audioPlayer.currentItem.duration.timescale);
    }
    if ((int)currentTime % 60 < 10) {
        _currentTimeLb.text = [NSString stringWithFormat:@"%d:0%d",(int)currentTime / 60, (int)currentTime % 60];
    } else {
        _currentTimeLb.text = [NSString stringWithFormat:@"%d:%d",(int)currentTime / 60, (int)currentTime % 60];
    }
    //
    if ((int)durationTime % 60 < 10) {
        _totalTimeLb.text = [NSString stringWithFormat:@"%d:0%d",(int)durationTime / 60, (int)durationTime % 60];
    } else {
        _totalTimeLb.text = [NSString stringWithFormat:@"%d:%d",(int)durationTime / 60, (int)durationTime % 60];
    }
    _progressView.value = 1.0 * currentTime / durationTime;
    NSLog(@"progress value ：%.2f",_progressView.value);
    
    //如果播放完，调用自动播放下一首
    if (_progressView.value > 0.999) {
        [self autoPlay];
    }
    
}

- (void)movieToEnd:(NSNotification*)noty
{
    if (_audioPlayer && [UserManager standerDefult].isBackPlay == NO) {
        AVPlayerItem *currentItem = noty.object;
        if ([_audioPlayer.currentItem isEqual:currentItem]) {
            [self autoPlay];
        }
    }
}

- (void)movieToFaile:(NSNotification *)noty
{
    if (_audioPlayer && [UserManager standerDefult].isBackPlay == NO) {
        AVPlayerItem *currentItem = noty.object;
        if ([_audioPlayer.currentItem isEqual:currentItem]) {
            [self autoPlay];
        }
    }
}

#pragma mark - 播放上一个
- (void)onSelectLastPlaying
{
    if (playModelType == PLayerModelType_Shunxu) {
        //顺序
        if (selectCurrentIndex == 0) {
            selectCurrentIndex = _musicArray.count;
        }
        selectCurrentIndex --;
    }
    else if (playModelType == PLayerModelType_SuiJi) {
        selectCurrentIndex = arc4random()%(_musicArray.count);
    }
    else if (playModelType == PLayerModelType_DanQu) {
        
    }
    
    [self updatePlayerSetting];
}

#pragma mark - 播放下一个
- (void)onSelectNextPlaying
{
    if (playModelType == PLayerModelType_Shunxu) {
        //顺序
        if (selectCurrentIndex == _musicArray.count - 1) {
            selectCurrentIndex = -1;
        }
        selectCurrentIndex ++;
    }
    else if (playModelType == PLayerModelType_SuiJi) {
        selectCurrentIndex = arc4random()%(_musicArray.count);
    }
    else if (playModelType == PLayerModelType_DanQu) {
        
    }
    
    [self updatePlayerSetting];
}

#pragma mark 自动进入下一首
- (void)autoPlay {
    //判断是否允许循环播放
    if (isCircle == YES) {
        selectCurrentIndex ++;
        if (selectCurrentIndex >= _musicArray.count - 1) {
            selectCurrentIndex = 0;
        }
    }
    
    [self updatePlayerSetting];
}

//更新播放器设置
- (void)updatePlayerSetting {
    
    if (_musicArray.count <= selectCurrentIndex || selectCurrentIndex < 0) return;
    
    [self setTitleView:[_musicArray[selectCurrentIndex] name]];
    
    //更新播放按钮状态
    isPlay = NO;
    
    //更新曲目
    [_audioPlayer pause];
    currentMusic = _musicArray[selectCurrentIndex];

    [self initDate];
    
    if (_playeringBt.selected == NO) {
        return;
    }
    NSURL *urlPath;
    if (currentMusic.isCacheData) {
        NSString *filePath = [MusicCachesDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",currentMusic.name,currentMusic.type]];
        urlPath = [NSURL fileURLWithPath:filePath];
    } else {
        urlPath = [NSURL URLWithString:currentMusic.urlPath];
    }
    
    AVPlayerItem *item =[AVPlayerItem playerItemWithURL:urlPath];
    [_audioPlayer replaceCurrentItemWithPlayerItem:nil];
    [_audioPlayer replaceCurrentItemWithPlayerItem:item];
    [_audioPlayer play];
    
    
}


#pragma mark - 音频播放选择模式
- (void)onSelectPlayType:(UIButton *)bt
{
    UIImage *typeImg = [UIImage imageNamed:@"shunxu"];
    NSString *type = @"1";
    if (playModelType  == PLayerModelType_Shunxu) {
        playModelType = PLayerModelType_SuiJi;
        typeImg = [UIImage imageNamed:@"suiji"];
        type = @"2";
    }
    else if (playModelType  == PLayerModelType_SuiJi) {
        playModelType = PLayerModelType_DanQu;
        typeImg = [UIImage imageNamed:@"danqu"];
        type = @"3";
    }
    else if (playModelType  == PLayerModelType_DanQu) {
        playModelType = PLayerModelType_Shunxu;
        typeImg = [UIImage imageNamed:@"shunxu"];
        type = @"1";
    }
    
    [bt setImage:typeImg forState:UIControlStateNormal];
    [bt setImage:typeImg forState:UIControlStateHighlighted];
    
    [[NSUserDefaults standardUserDefaults]setValue:type forKey:@"player_z_model_type"];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

#pragma mark -  下载
- (void)onSelectDownMusic
{
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model.UserID) {
        MBLoginController *registerLastVC = [[MBLoginController alloc] init];
        registerLastVC.formBaseViewCtrl = NSStringFromClass([self class]);
        [self.navigationController pushViewController:registerLastVC animated:YES];
        return;
    }
    
    if (_downBt.selected == YES) {
        //已下载
        return;
    }
    _downBt.enabled = NO;
    
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [formatter stringFromDate:date];
    
    [[DownManager staredInstance]downloadWithUrl:currentMusic.urlPath
                                   withMusicName:[NSString stringWithFormat:@"%@.%@",currentMusic.name,currentMusic.type]
                                      withUserID:[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"UserID"]]
                                     withMusicID:currentMusic.ID
                                     withContent:currentMusic.Sentiment
                                   withMusicType:[NSString stringWithFormat:@"%@",currentMusic.musicType]
                                      withPicUrl:currentMusic.CoverPath
                                        withDate:dateString
                                     withUserPic:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,[_dataDic objectForKey:@"FaceImg"]]
                                    withUserNick:[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"NickName"]]
                                        withSize:[NSString stringWithFormat:@"%@",currentMusic.Capacity]
                                    withProgress:nil withStats:nil];
    [ErrorMessage showErrorMessage:@"开始下载" inView:self.view];
    
    //下载次数接口
    [MBProgressHUD showHUDAddedTo:_scrollView animated:YES];
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:FAMILYSHOW_DOWN_LOADNUM_URL_PATH,model.UserID,currentMusic.ID,@2] view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:_scrollView animated:YES];
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:_scrollView animated:YES];
    }];
}

#pragma mark - 点赞、收藏
- (void)onSelectItemForZanOrCollection:(UIButton *)bt
{
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model) {
        MBLoginController *registerLastVC = [[MBLoginController alloc] init];
        registerLastVC.formBaseViewCtrl = NSStringFromClass([self class]);
        [self.navigationController pushViewController:registerLastVC animated:YES];
        return;
    }
    
    NSString *PraiseStatus = [_selectPraiseDic objectForKey:@"PraiseStatus"];
    NSString *CollectStatus = [_selectPraiseDic objectForKey:@"CollectStatus"];
    NSString *PraiseNum = [_selectPraiseDic objectForKey:@"PraiseNum"];
    
    UIImageView *topImgView1 = (UIImageView *)[self.view viewWithTag:10000];
    UIImageView *topImgView2 = (UIImageView *)[self.view viewWithTag:10001];
    UILabel *zanLb1 = (UILabel *)[self.view viewWithTag:11000];
    UILabel *zanLb2 = (UILabel *)[self.view viewWithTag:11001];
    
    NSInteger index = bt.tag - 700;
    switch (index) {
        case 0:
        {
            //点赞
            if (!_selectPraiseDic) return;
            
            if ([PraiseStatus intValue] == 1) {
                //点亮,取消点赞
                [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:FORUM_CANCEL_ZAN_URL_PATH,model.UserID,currentMusic.ID,@1] view:nil success:^(id json) {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                    
                    if ([json[@"ret_code"] integerValue]==0) {
                        //取消点赞成功
                        topImgView1.image = [UIImage imageNamed:@"zan_qzxbf"];
                        zanLb1.textColor = [UIColor whiteColor];
                        zanLb1.text = [NSString stringWithFormat:@"点赞(%d)",[PraiseNum intValue]-1];
                        [_selectPraiseDic setObject:@"0" forKey:@"PraiseStatus"];
                        [_selectPraiseDic setObject:[NSString stringWithFormat:@"%d",[PraiseNum intValue]-1] forKey:@"PraiseNum"];
                    } else {
                        [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
                    }
                } failure:^(NSError *error) {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                }];
                
            } else {
                //取消
                [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:FORUM_ZAN_URL_PATH,model.UserID,currentMusic.ID,@1] view:nil success:^(id json) {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                    
                    if ([json[@"ret_code"] integerValue]==0) {
                        //点赞成功
//                        topImgView1.image = [UIImage imageNamed:@"zan2_qzxbf"];
//                        zanLb1.textColor = TINK_COLOR;
                        zanLb1.text = [NSString stringWithFormat:@"点赞(%d)",[PraiseNum intValue]+1];
                        [_selectPraiseDic setObject:@"1" forKey:@"PraiseStatus"];
                        [_selectPraiseDic setObject:[NSString stringWithFormat:@"%d",[PraiseNum intValue]+1] forKey:@"PraiseNum"];
                    } else {
                        [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
                    }
                } failure:^(NSError *error) {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                }];
                
            }
        }
            break;
        case 1:
        {
            //收藏
            if (!_selectPraiseDic) return;
            
            if ([CollectStatus intValue] == 1) {
                //点亮,取消收藏
                [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:SPACE_COLLECTION_REMOVE_URL_PATH,model.UserID,currentMusic.ID,@3] view:nil success:^(id json) {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                    
                    if ([json[@"ret_code"] integerValue]==0) {
                        //取消点赞成功
                        topImgView2.image = [UIImage imageNamed:@"shoucang_qzxbf"];
                        zanLb2.textColor = [UIColor whiteColor];
                        zanLb2.text = @"收藏";
                        [_selectPraiseDic setObject:@"0" forKey:@"CollectStatus"];
                    } else {
                        [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
                    }
                } failure:^(NSError *error) {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                }];
                
            } else {
                //取消，收藏吧
                [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:SPACE_COLLECTION_COLLECION_URL_PATH,model.UserID,currentMusic.ID,@3] view:nil success:^(id json) {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                    
                    if ([json[@"ret_code"] integerValue]==0) {
                        //点赞成功
//                        topImgView2.image = [UIImage imageNamed:@"shoucang2_qzxbf"];
//                        zanLb2.textColor = TINK_COLOR;
                        zanLb2.text = @"已收藏";
                        [_selectPraiseDic setObject:@"1" forKey:@"CollectStatus"];
                        
                        AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
                        if ([UserManager standerDefult].isLoadMusic == YES && app.networkStatus == ReachableViaWiFi) {
                            [self onSelectDownMusic];
                        }
                    } else {
                        [ErrorMessage showErrorMessage:json[@"message"] inView:self.view];
                    }
                } failure:^(NSError *error) {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                }];
            }
        }
            break;
            
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -远程控制
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent
{
    if (receivedEvent.type == UIEventTypeRemoteControl) {
        switch (receivedEvent.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:
                [_audioPlayer pause];
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                [self onSelectNextPlaying];
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                [self onSelectLastPlaying];
                break;
            case UIEventSubtypeRemoteControlPause:
                
                [_audioPlayer pause];
                break;
            case UIEventSubtypeRemoteControlPlay:
                [_audioPlayer play];
                break;
            default:
                break;
        }
    }
}

#pragma mark - ScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float y = scrollView.contentOffset.y;
    //    NSLog(@"滑动位移:%.2f",y);
    if (y > 0 && y < 276*PERCEN*2) {
        //变深色
        _navBarView.backgroundColor = [UIColorFromHex(0x333333)colorWithAlphaComponent:.35];
    }
    else if ((y <= 0 && y > -276*PERCEN)) {
        //变淡
        _navBarView.backgroundColor = [UIColorFromHex(0x333333)colorWithAlphaComponent:.0];
    }
}

@end
